package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.common.MD5Utils;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.utils.RegularUtils;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.library.widget.HSEditText;
import com.hundsun.yr.universal.library.widget.HSPassedwordEditText;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.InitializePresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.utils.Logs;
import com.hundsun.yr.universal.p2p.view.base.InitializeView;
import com.hundsun.yr.universal.p2p.widget.ValidateButton;

import org.json.JSONException;
import org.json.JSONObject;

import de.greenrobot.event.EventBus;

/**
 * Created by xx on 2017/6/12.
 * 重置登录密码
 */
public class ResettingPasswordActivity extends BaseActivity implements InitializeView,View.OnClickListener{
    private TextView tv_phone;
    private HSEditText et_vcode;
    private ValidateButton btn_vcode;
    private HSPassedwordEditText et_originalpwd;
    private String phone;
    private HSButton btn_update_pay_pwd;
    private String zwgt_data;
    private RequestPresenter mPresenter;
    private String passwd;
    private String vcode;
    private HSPassedwordEditText et_voice_verification_code;

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_resettingpassword;
    }

    @Override
    protected void initViewsAndEvents() {
        findView();
        toolbar_title.setText(getString(R.string.rename_login_password));
        btn_vcode.onClick(btn_vcode);
        tv_phone.setText(getString(R.string.send_vcode_to_phone,CommTool.encodPhone(phone)));
        mPresenter = new InitializePresenterImpl(mContext,this);
        btn_vcode.setOnClickListener(this);
        btn_update_pay_pwd.setOnClickListener(this);
        et_voice_verification_code.setOnClickListener(this);
    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        phone = extras.getString("phone");
    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    public void findView(){
        tv_phone = findView(R.id.tv_phone);
        et_vcode = findView(R.id.et_vcode);
        btn_vcode = findView(R.id.btn_vcode);
//        et_originalpwd = findView(R.id.et_originalpwd);
        btn_update_pay_pwd = findView(R.id.btn_update_pay_pwd);
        et_voice_verification_code = findView(R.id.et_voice_verification_code);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_vcode://发送短信
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map = new JSONObject();
                try {
                    map.put("type","2");
                    map.put("phone", phone);
                    map.put("channel","0");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                JSONObject jsonParams = CommTool.getJSONParams(map, mConfigSP.getShareProf(TaskConstants.TASK_CONFIG), zwgt_data);
                mPresenter.loadingData(TaskConstants.TASK_SENDSMS, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_index_test)
                        + TaskConstants.TASK_SENDSMS, Constants.EVENT_DATA, jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST,
                        mContext.getString(R.string.api_index_test) + TaskConstants.TASK_SENDSMS, "", mConfigSP.getShareProf(TaskConstants.TASK_CONFIG), zwgt_data),false);
                break;
            case R.id.btn_update_pay_pwd://重置密码
                vcode = et_vcode.getText().toString();
                passwd = et_voice_verification_code.getText();
                if (vcode == null || vcode.isEmpty()){
                    showToast(getString(R.string.empty_vcode));
                    return;
                }

                //验证码格式不正确
                if (!RegularUtils.isLength(vcode,6)) {
                    showToast(R.string.error_vcode);
                    return;
                }

                //验证密码不能为空
                if (passwd == null || passwd.isEmpty()){
                    showToast(R.string.empty_pwd);
                    return;
                }

                //验证密码是否正确
                if (!CommTool.checkPwd(passwd)){
                    showToast(R.string.register_pass_rightcheck);
                    return;
                }

                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map2 = new JSONObject();
                try {
                    map2.put("phone", phone);
                    map2.put("vcode",vcode);
                    map2.put("passwd", MD5Utils.getMD5String(passwd));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                JSONObject jsonParams2 = CommTool.getJSONParams(map2, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                mPresenter.loadingData(TaskConstants.TASK_RESETPWD, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_index_test)
                        + TaskConstants.TASK_RESETPWD, Constants.EVENT_DATA, jsonParams2, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST,
                        mContext.getString(R.string.api_index_test) + TaskConstants.TASK_RESETPWD, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data),false);

                break;
            case R.id.et_voice_verification_code:
                //发送语音短信
                break;
        }
    }

    @Override
    public <T> void initializeViews(String task_name, T data) {
        switch (task_name){
            case TaskConstants.TASK_SENDSMS://短信发送成功

                break;
            case TaskConstants.TASK_RESETPWD://更改密码
                showToast(R.string.modify_success);
                finish();

                if (BaseAppManager.getInstance().getActivity(MineSettingActivity.class) != null){
                    BaseAppManager.getInstance().getActivity(MineSettingActivity.class).finish();
                }

                if (BaseAppManager.getInstance().getActivity(PasswordMangerActivity.class) != null){
                    BaseAppManager.getInstance().getActivity(PasswordMangerActivity.class).finish();
                }

                sharedPreferences.putBoolean(UserInfo.ISLOGIN, false);
                sharedPreferences.clear(mContext);

                EventBus.getDefault().post(new EventCenter(Constants.EVENT_DATA_LOGIN_EXPIRED2));//修改密码成功跳转到第一页然后登陆

                readyGoThenKill(LoginActivity.class);
                break;
        }
    }

    @Override
    public <T> void initializeError(String task_name, T data) {

    }
}
